﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quanlytruongtruongtieuhoc
{
    public partial class frmCapnhatphancongcn : Form
    {
        string magv, malop,nam;
        public frmCapnhatphancongcn()
        {
            InitializeComponent();
        }

        private void frmCapnhatphancongcn_Load(object sender, EventArgs e)
        {
            try
            {
                magv = frmPhanconggvchunhiem.mgv;
                malop = frmPhanconggvchunhiem.mlop;
                nam=frmPhanconggvchunhiem.layNam();
                SqlDataAdapter ap = new SqlDataAdapter("select ma_gv from giao_vien where ma_lv='CN' and ma_gv not in(select ma_gv from chu_nhiem where nam_hoc=(select max(nam_hoc) from nam_hoc))", clsKetnoi.con);
                DataTable gv = new DataTable();
                gv.Clear();
                ap.Fill(gv);
                cbMalop.DataSource = gv;
                cbMalop.DisplayMember = "ma_gv";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btOk_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult kq = new DialogResult();
                kq = MessageBox.Show("Có chắc thay đổi không ?", "Thông tin", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (kq == DialogResult.OK)
                {
                    if (cbMalop.Text == "")
                    {
                        MessageBox.Show("Chưa chọn mã lớp.", "Thông tin", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    }
                    else
                    {
                        SqlCommand com = new SqlCommand("update chu_nhiem set ma_gv='"+cbMalop.Text+"' where nam_hoc='"+nam+"' and ma_khoi='"+malop.Substring(0,1)+"' and ma_lop='"+malop.Substring(2,1)+"'" , clsKetnoi.con);
                        com.ExecuteNonQuery();
                        SqlCommand com1 = new SqlCommand("update giang_day set ma_gv='" + cbMalop.Text + "' where nam_hoc='" + nam + "' and ma_gv='" + magv + "'", clsKetnoi.con);
                        com1.ExecuteNonQuery();
                        MessageBox.Show("Cập nhật thành công.", "Thông tin", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                        this.Hide();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
